package im.yisme.view.activities;

import java.util.ArrayList;

import im.yisme.R;
import im.yisme.controler.service.CtrlServiceProvider;
import im.yisme.controler.service.RecordCtrlService;
import im.yisme.model.service.ModelServiceProvider;
import im.yisme.model.service.RecordSystemService;
import im.yisme.view.service.ViewService;
import im.yisme.view.service.ViewServiceProvider;
import im.yisme.view.tasks.UploadTask;
import im.yisme.view.ui.RecordResultView;
import im.yisme.view.ui.component.OnClickListener;
import im.yisme.view.ui.component.YButton;
import im.yisme.view.ui.util.ButtonEventListener;
import im.yisme.view.ui.util.YToast;
import im.yisme.vo.LyricsVO;
import im.yisme.vo.MusicVO;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.PointF;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

public class RecordOverActivity extends Activity {

	private ViewService viewCtrl;
	private RecordCtrlService recordCtrl;
	private RecordSystemService recordModel;
	
	private YButton uploadButton, againButton, toLyricsButton, backButton;
	private ButtonEventListener eventListener;
	private RecordResultView recordResultView;

	protected void onCreate(Bundle savedInstanecBundle) {
		super.onCreate(savedInstanecBundle);
		
		viewCtrl = ViewServiceProvider.getViewService();
        recordCtrl = CtrlServiceProvider.getRecordCtrlService();
        recordModel = ModelServiceProvider.getRecordSystemService();
        
		init();
		setContentView(recordResultView);
	}

	protected void onResume() {
		super.onResume();
		viewCtrl.updateTopActivity(this);
	}

	protected void onDestroy() {
		super.onDestroy();
		recordResultView.stop();
	}

	protected void onPause() {
		super.onPause();
		recordResultView.pause();
	}

	protected void onRestart() {
		super.onRestart();
		recordResultView.resume();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return eventListener.onButtonEvent(event);
	}

	private void init() {
		// TODO Auto-generated method stub
		recordResultView = new RecordResultView(this);
		initButtons();
		initButtonListeners();
		registerButtons();
		
		
		recordResultView.setData(recordModel.getNoteNum(), recordModel.getDuration());
	}

	private void registerButtons() {
		// TODO Auto-generated method stub
		eventListener = new ButtonEventListener();
		eventListener.registerButton(uploadButton);
		eventListener.registerButton(againButton);
		eventListener.registerButton(toLyricsButton);
		eventListener.registerButton(backButton);
	}

	private void initButtonListeners() {
		// TODO Auto-generated method stub
		uploadButton.setOnClickListener(new OnClickListener() {

			public void OnClick() {
				// TODO Auto-generated method stub
				recordCtrl.finishRecord();
				LyricsVO lyricsVO = recordModel.getRecordedLyrics();
				
				YToast.makeText(RecordOverActivity.this, "正在上传乐谱...", 700).show();
				
				UploadTask task = new UploadTask();
				task.execute(lyricsVO.getId());
			}
		});

		againButton.setOnClickListener(new OnClickListener() {

			public void OnClick() {
				// TODO Auto-generated method stub
				Intent intent = new Intent(RecordOverActivity.this,
						RecordActivity.class);
				
				Intent intent1 = getIntent();		
				MusicVO musicVO = (MusicVO) intent1.getSerializableExtra("musicVO");
				
				intent.putExtra("musicVO", musicVO);
				
				viewCtrl.jumpTo(intent);
			}
		});

		toLyricsButton.setOnClickListener(new OnClickListener() {

			public void OnClick() {
				// TODO Auto-generated method stub
				recordCtrl.finishRecord();
				Intent intent = new Intent(RecordOverActivity.this,
						LyricsLibraryActivity.class);
				viewCtrl.jumpTo(intent);
			}
		});

		backButton.setOnClickListener(new OnClickListener() {

			public void OnClick() {
				// TODO Auto-generated method stub
				recordCtrl.finishRecord();
				Intent intent = new Intent(RecordOverActivity.this,
						RecordMusicListActivity.class);
				viewCtrl.jumpTo(intent);
			}
		});

	}

	private void initButtons() {
		// TODO Auto-generated method stub
		Resources r = getResources();

		ArrayList<PointF> uploadPointFs = new ArrayList<PointF>();
		uploadPointFs.add(new PointF(r
				.getDimension(R.dimen.record_result_upload_range_x), r
				.getDimension(R.dimen.record_result_upload_range_y)));
		uploadPointFs.add(new PointF(r
				.getDimension(R.dimen.record_result_upload_range_x), r
				.getDimension(R.dimen.record_result_upload_range_y)
				+ r.getDimension(R.dimen.record_result_upload_height)));
		uploadPointFs.add(new PointF(r
				.getDimension(R.dimen.record_result_upload_range_x)
				+ r.getDimension(R.dimen.record_result_upload_width), r
				.getDimension(R.dimen.record_result_upload_range_y)
				+ r.getDimension(R.dimen.record_result_upload_height)));
		uploadPointFs.add(new PointF(r
				.getDimension(R.dimen.record_result_upload_range_x)
				+ r.getDimension(R.dimen.record_result_upload_width), r
				.getDimension(R.dimen.record_result_upload_range_y)));
		uploadButton = new YButton();
		uploadButton.setPoints(uploadPointFs);

		ArrayList<PointF> againPointFs = new ArrayList<PointF>();
		againPointFs.add(new PointF(r
				.getDimension(R.dimen.record_result_again_range_x), r
				.getDimension(R.dimen.record_result_again_range_y)));
		againPointFs.add(new PointF(r
				.getDimension(R.dimen.record_result_again_range_x), r
				.getDimension(R.dimen.record_result_again_range_y)
				+ r.getDimension(R.dimen.record_result_again_height)));
		againPointFs.add(new PointF(r
				.getDimension(R.dimen.record_result_again_range_x)
				+ r.getDimension(R.dimen.record_result_again_width), r
				.getDimension(R.dimen.record_result_again_range_y)
				+ r.getDimension(R.dimen.record_result_again_height)));
		againPointFs.add(new PointF(r
				.getDimension(R.dimen.record_result_again_range_x)
				+ r.getDimension(R.dimen.record_result_again_width), r
				.getDimension(R.dimen.record_result_again_range_y)));
		againButton = new YButton();
		againButton.setPoints(againPointFs);

		ArrayList<PointF> toLyricsPointFs = new ArrayList<PointF>();
		toLyricsPointFs.add(new PointF(r
				.getDimension(R.dimen.record_result_tolyrics_range_x), r
				.getDimension(R.dimen.record_result_tolyrics_range_y)));
		toLyricsPointFs.add(new PointF(r
				.getDimension(R.dimen.record_result_tolyrics_range_x), r
				.getDimension(R.dimen.record_result_tolyrics_range_y)
				+ r.getDimension(R.dimen.record_result_tolyrics_height)));
		toLyricsPointFs.add(new PointF(r
				.getDimension(R.dimen.record_result_tolyrics_range_x)
				+ r.getDimension(R.dimen.record_result_tolyrics_width), r
				.getDimension(R.dimen.record_result_tolyrics_range_y)
				+ r.getDimension(R.dimen.record_result_tolyrics_height)));
		toLyricsPointFs.add(new PointF(r
				.getDimension(R.dimen.record_result_tolyrics_range_x)
				+ r.getDimension(R.dimen.record_result_upload_width), r
				.getDimension(R.dimen.record_result_tolyrics_range_y)));
		toLyricsButton = new YButton();
		toLyricsButton.setPoints(toLyricsPointFs);

		ArrayList<PointF> backPointFs = new ArrayList<PointF>();
		backPointFs.add(new PointF(r
				.getDimension(R.dimen.record_result_back_range_x), r
				.getDimension(R.dimen.record_result_back_range_y)));
		backPointFs.add(new PointF(r
				.getDimension(R.dimen.record_result_back_range_x), r
				.getDimension(R.dimen.record_result_back_range_y)
				+ r.getDimension(R.dimen.record_result_back_height)));
		backPointFs.add(new PointF(r
				.getDimension(R.dimen.record_result_back_range_x)
				+ r.getDimension(R.dimen.record_result_back_width), r
				.getDimension(R.dimen.record_result_back_range_y)
				+ r.getDimension(R.dimen.record_result_back_height)));
		backPointFs.add(new PointF(r
				.getDimension(R.dimen.record_result_back_range_x)
				+ r.getDimension(R.dimen.record_result_back_width), r
				.getDimension(R.dimen.record_result_back_range_y)));
		backButton = new YButton();
		backButton.setPoints(backPointFs);
	}
}
